<?php

/**
 * 订单评价列表
 * @author renjianhong
 * @date 2021-8-03 16:47
 */
namespace Modules\Order\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Order\Http\Controllers\Controller;
use Modules\Order\Entities\OrderComment;
use Modules\Order\Entities\Order;
use Modules\Order\Entities\OrderDetail;


class OrderCommentController extends Controller
{
     /**
     * 订单评价列表
     */
    public function list(Request $request)
    { 
        return view('order::admin.order_comment.list');
    }

    /**
     * AJAX获取数据
     */
    public function ajaxList(Request $request , OrderComment $OrderCommentModel)
    {
        $pagesize = $request->input('limit'); // 每页条数
        $page = $request->input('page',1);//当前页
        $goods_name = $request->input('goods_name');
        $nickname = $request->input('nickname');
        $where = [
            ['order_detail.goods_name', 'like', '%'.$goods_name.'%'],
            ['users.nickname', 'like', '%'.$nickname.'%'],
            ['order_comment.is_delete', '=', '0'],
        ];

        //获取总条数
        $count = $OrderCommentModel
            ->leftJoin('order_detail', 'order_detail.id', '=', 'order_comment.order_detail_id')
            ->leftJoin('users', 'users.id', '=', 'order_comment.user_id')
            ->where($where)
            ->count();
        
        //求偏移量
        $offset = ($page-1)*$pagesize;
        $list = $OrderCommentModel
                ->leftJoin('order_detail', 'order_detail.id', '=', 'order_comment.order_detail_id')
                ->leftJoin('users', 'users.id', '=', 'order_comment.user_id')   
                ->where($where)
                ->select('order_comment.*','users.nickname','users.avatar','order_detail.goods_name','order_detail.goods_pic')
                ->orderByDesc('id')->offset($offset)->limit($pagesize)->get();
        foreach($list as $key => $v){
            $is_domain = $OrderCommentModel->getIsDomain($v['avatar']);
            if($is_domain){
                $list[$key]['avatar'] = $v['avatar'];
            }else{
                $list[$key]['avatar'] = $OrderCommentModel->getShowImage($v['avatar']);
            }
            $goods_pic = $OrderCommentModel->getIsDomain($v['goods_pic']);
            if($goods_pic){
                $list[$key]['goods_pic'] = $v['goods_pic'];
            }else{
                $list[$key]['goods_pic'] = $OrderCommentModel->getShowImage($v['goods_pic']);
            }
        }
        return $this->success(compact('list', 'count'));    
        
    }

    /**
     * 回复评价
     */
    public function reply(Request $request, OrderComment $OrderCommentModel){
        if($request->isMethod('post')){
            $data = $request->input();
            
            $order_comment = $OrderCommentModel->where('id',$data['id'])->first();
            if(!$order_comment){
                return $this->failed('评价不存在');
            }
            $order_comment->reply_content = $data['reply_content'];
            if($order_comment->save()){
                return $this->success('操作成功');
            }else{
                return $this->failed('操作失败');
            }
        }
        $id = $request->input('id');
        $info = $OrderCommentModel->where('order_comment.id',$id)
                ->leftJoin('order_detail', 'order_detail.id', '=', 'order_comment.order_detail_id')
                ->leftJoin('users', 'users.id', '=', 'order_comment.user_id')
                ->select('order_comment.*','users.nickname','users.avatar','order_detail.goods_name','order_detail.goods_pic')
                ->first(); 
        if($info){
            $is_domain = $OrderCommentModel->getIsDomain($info->avatar);
            if($is_domain){
                $info->avatar = $info->avatar;
            }else{
                $info->avatar = $OrderCommentModel->getShowImage($info->avatar);
            }
            $goods_pic = $OrderCommentModel->getIsDomain($info->goods_pic);
            if($goods_pic){
                $info->goods_pic = $info->goods_pic;
            }else{
                $info->goods_pic = $OrderCommentModel->getShowImage($info->goods_pic);
            }
        }
        return view('order::admin.order_comment.reply',compact('info'));
    }

    /**
     * 删除
     */
    public function delete(Request $request, OrderComment $OrderCommentModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $OrderCommentModel->where('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('缺少参数id');
            }
        }
    }

    /**
     * 批量删除
     */
    public function batchDelete(Request $request, OrderComment $OrderCommentModel){
        if($request->isMethod('post')){
            $id = $request->input('id');
            if($id){
                $reulst = $OrderCommentModel->whereIn('id',$id)->update(['is_delete'=>1]);
                if($reulst){
                    return $this->success('操作成功');
                }else{
                    return $this->failed('操作失败');
                }
            }else{
                return $this->failed('请勾选要删除的数据');
            }
        }
    }

    /**
     * 是否隐藏
     */
    public function is_hide(Request $request, OrderComment $OrderCommentModel)
    {
        if($request->isMethod('post')){
            $is_hide = $request->input('is_hide');
            $id = $request->input('id');
            if($is_hide == 'true'){
                //启用
                $reulst = $OrderCommentModel->where('id',$id)->update(['is_hide'=>1]);
            }else{
                //禁用
                $reulst = $OrderCommentModel->where('id',$id)->update(['is_hide'=>0]);
            }
            
            if($reulst){
                return $this->success('操作成功');
            }else{
                return $this->failed('操作失败');
            }
        }
    }

}